<template>
	<view class="container">
		<!-- 日历组件 -->
		<uni-calendar 
			:selected="selectedDates" 
			@monthSwitch="handleMonthChange" 
			@change="handleDateClick"
			class="custom-calendar animated-item" 
			style="animation-delay: 0.1s"
		></uni-calendar>

		<!-- 打卡记录 -->
		<view class="record-list animated-item" v-if="showRecords" :style="{animationDelay: '0.2s'}">
			<view class="section-title">
				<u-icon name="calendar" size="20" color="#409EFF"></u-icon>
				<text class="title-text"> 打卡记录</text>
			</view>
			<view 
				v-for="(item, index) in dailyRecords" 
				:key="item.punchId" 
				class="record-item animated-item"
				:style="{animationDelay: `${0.3 + index*0.1}s`}"
			>
				<view class="item-left">
					<u-icon name="map-fill" color="#409EFF" size="24"></u-icon>
					<text class="name">{{ item.location_name }}</text>
				</view>
				<text class="time">{{ item.createTime }}</text>
			</view>
			<view v-if="dailyRecords.length === 0" class="empty-tip animated-item" style="animation-delay: 0.3s">
				<u-icon name="info-circle" color="#909399" size="24"></u-icon>
				<text>该日期没有打卡记录</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		listStudypunch,
		applistStudypunch
	} from '@/api/studytourism/studypunch'
	export default {
		data() {
			return {
				selectedDate: '',
				showRecords: false,
				selectedDates: [],
				dailyRecords: []
			}
		},
		onLoad() {
			this.getPunchList()
		},
		methods: {
			async getPunchList() {
				const params = {
					pageNum: 1,
					pageSize: 25
				}
				const res = await listStudypunch(params)
				if (res.code === 200) {
					this.selectedDates = res.rows.map(item => ({
						...item,
						date: item.punchDate,
						info: '已打卡',
						customStyle: {
							backgroundColor: '#f0f9eb',
							color: '#67c23a'
						}
					}))
				}
			},
			handleMonthChange(e) {
				console.log('月份切换:', e);
			},
			async handleDateClick(e) {
				this.selectedDate = e.fulldate;
				this.showRecords = true;
				const params = {
					punchDate: this.selectedDate,
				}
				this.dailyRecords = [];
				const res = await applistStudypunch(params)
				if (res.code === 200) {
					res.rows.forEach(item => {
						if (item.punchDate === this.selectedDate)
							item.names.forEach(nameObj => {
								this.dailyRecords.push({
									...item,
									location_name: nameObj.name
								});
							});
					});
				}
			}
		},
		mounted() {
			this.showRecords = false;
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 20rpx;
		background-color: #f5f7fa;
		min-height: 100vh;
	}

	.custom-calendar {
		margin-bottom: 30rpx;
		background-color: #fff;
		border-radius: 8rpx;
		box-shadow: 0 2rpx 12rpx 0 rgba(0, 0, 0, 0.1);
	}

	.section-title {
		display: flex;
		align-items: center;
		padding: 20rpx 0;
		margin-bottom: 20rpx;
		border-bottom: 1rpx solid #ebeef5;
		.title-text {
			margin-left: 10rpx;
			font-size: 32rpx;
			color: #303133;
			font-weight: bold;
		}
	}

	.record-list {
		background-color: #fff;
		padding: 30rpx;
		border-radius: 8rpx;
		box-shadow: 0 2rpx 12rpx 0 rgba(0, 0, 0, 0.1);
	}

	.record-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 25rpx 0;
		border-bottom: 1rpx solid #ebeef5;
		&:last-child {
			border-bottom: none;
		}
		.item-left {
			display: flex;
			align-items: center;
		}
		.name {
			margin-left: 15rpx;
			font-size: 30rpx;
			color: #606266;
		}
		.time {
			font-size: 28rpx;
			color: #909399;
		}
	}

	.empty-tip {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 40rpx 0;
		color: #909399;
		font-size: 28rpx;
		text {
			margin-left: 10rpx;
		}
	}

	/* 新增动画样式 */
	.animated-item {
		opacity: 0;
		animation: fadeInUp 0.6s ease forwards;
	}

	@keyframes fadeInUp {
		from {
			opacity: 0;
			transform: translateY(20px);
		}
		to {
			opacity: 1;
			transform: translateY(0);
		}
	}
</style>